package com.hngy.xpq.android_practice.merchant_main.Comm;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.hngy.xpq.android_practice.R;
import com.hngy.xpq.android_practice.adapter.CommAdapter;
import com.hngy.xpq.android_practice.bean.Comm;
import com.hngy.xpq.android_practice.database.SQLiteComm;
import com.hngy.xpq.android_practice.http.TcpClient;
import com.hngy.xpq.android_practice.ui.home.User_Comm;

import java.util.List;

public class Comm_query extends AppCompatActivity {

    SharedPreferences preferences,comm;
    TextView textView;
    GridView item;
    List<Comm> list;
    SQLiteComm sqLiteComm;
    CommAdapter adapter;
    TextView text_ck;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.comm_query);

        preferences = super.getSharedPreferences("merchants",MODE_PRIVATE);
        comm = super.getSharedPreferences("comm",MODE_PRIVATE);

        item = findViewById(R.id.comm_avatar);
        text_ck = findViewById(R.id.text_ck);
        textView = findViewById(R.id.text_ck);
        textView.setText(comm.getString("name","") + "的店铺");
        //信息获取
        InitData();
    }

    private void InitData() {
        sqLiteComm = new SQLiteComm(this);
        showQueryData();
        Intent intent = getIntent();
        String quan = intent.getStringExtra("quan");
        if (TextUtils.isEmpty(quan)){
            item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(Comm_query.this,CommMerchantActivity.class);
                    Comm c = list.get(position);
                    intent.putExtra("id",c.getId());
                    intent.putExtra("name",c.getComm_name());
                    intent.putExtra("types",c.getComm_types());
                    intent.putExtra("price",c.getComm_price());
                    intent.putExtra("des",c.getComm_des());
                    intent.putExtra("status",c.getStatus());
                    startActivity(intent);
                    finish();
                }
            });
            item.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                    AlertDialog dialog;
                    AlertDialog.Builder builder = new AlertDialog.Builder(Comm_query.this)
                            .setMessage("是否删除商品?")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Comm comm = list.get(position);
                                    String information = "SC001//" + comm.getComm_name();
                                    String a = new TcpClient().Transits(information);
                                    if (a.equals("商品删除成功") && sqLiteComm.deleteData(comm.getComm_name())){
                                        list.remove(position);
                                        adapter.notifyDataSetChanged();
                                        Toast.makeText(Comm_query.this,"删除成功!!",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    dialog = builder.create();
                    dialog.show();
                    showQueryData();
                    return true;
                }
            });
        } else {
            item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(Comm_query.this, User_Comm.class);
                    Comm c = list.get(position);
                    SharedPreferences.Editor editor = comm.edit();
                    editor.putString("name",c.getMerchant_name());
                    editor.putString("price",c.getComm_price());
                    editor.putString("phone",c.getComm_phone());
                    editor.putString("comm_name",c.getComm_name());
                    editor.putString("position", String.valueOf(position));
                    editor.putString("math","");
                    editor.commit();
                    SharedPreferences.Editor editor1 = preferences.edit();
                    editor1.putString("name",c.getMerchant_name());
                    editor1.commit();
                    startActivity(intent);
                }
            });
        }
    }

    private void showQueryData() {
        if (list != null){
            list.clear();
        }
        //获得所有商品信息
        list = sqLiteComm.CommMerchantQuery(comm.getString("name",""));
        //适配器的数据安置
        adapter = new CommAdapter(this,list);
        item.setAdapter(adapter);
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1&&resultCode==2){
            showQueryData();
        }
    }

}